package com.dunqi.gpm.cangxi.develop.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.WebActivity;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.develop.activity.ProjectTrendDetailActivity;
import com.dunqi.gpm.cangxi.develop.adapter.NewsAdapter;
import com.dunqi.gpm.cangxi.develop.bean.ProjectNews;
import com.dunqi.gpm.cangxi.home.activity.ProjectDetailActivity;
import com.dunqi.gpm.cangxi.home.activity.WorkInstructionsActivity;
import com.dunqi.gpm.cangxi.home.bean.Page;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.fragment.HomeFragment;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;

import java.util.ArrayList;

import butterknife.BindView;


public class NewsDevelopFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    ArrayList<ProjectNews> data = new ArrayList<>();
    private NewsAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    @SuppressLint("CheckResult")
    private void net() {
        int userLevel=UserUtil.getLevelType();
        ApiFactory.getInstance()
                .getAppNewest(userLevel!=1 && userLevel!=2, page)
                .compose(RxSchedulers.<Page<ProjectNews>>io_main())
                .subscribe(pages -> {
                    SwipeUtil.loadCompleted(swipeRefresh);
                    ArrayList<ProjectNews> list = pages.getList();
                    if (page == 1) {
                        data.clear();
                    }
                    data.addAll(list);
                    adapter.notifyDataSetChanged();
                    page++;

                    if (pages.isHasNextPage()) {
                        adapter.loadMoreComplete();
                    } else {
                        adapter.loadMoreEnd();
                    }
                }, throwable -> {
                    adapter.loadMoreFail();
                    SwipeUtil.loadCompleted(swipeRefresh);
                });
    }

    @Override
    protected void initUiAndListener() {

        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new NewsAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(context));

        SwipeUtil.init(swipeRefresh);

        swipeRefresh.setOnRefreshListener(this);
        adapter.setEmptyView(new EmptyView(context));
        adapter.setOnLoadMoreListener(this, recycler);

        adapter.setOnItemClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news_develop;
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        HomeFragment.homeMenuId=Constance.MainMenu.MENU_NEWS_DEVELOP;
        ProjectNews projectNews = data.get(position);

        Byte objectType = projectNews.getObjectType();
        Long mainObjectId = projectNews.getMainObjectId();
        if (objectType == 1 || objectType == 10) {
            long instruction_type;
            if (objectType == 1) {
                instruction_type = Constance.INSTRUCTION_TYPE_PROJECT;
            } else {
                instruction_type = Constance.INSTRUCTION_TYPE_PROJECT_WORK_TREND;
            }
            WorkInstructionsActivity.start(context, mainObjectId, instruction_type);
        } else if (objectType == 2) {
            Project project = new Project();
            project.setProjectId(mainObjectId);
            //HomeFragment.homeMenuId = 20;
            ProjectDetailActivity.start(context, project, 8);

        } else if (objectType == 3) {

            String msgType = projectNews.getMsgType();
            String content = projectNews.getDetail();

            if ("county_leader_link".equals(msgType) || "county_sec_link".equals(msgType)) {
                WebActivity.start(context, content);
            } else {
                //WorkDevelopDetailActivity.start(context, projectNews);
                ProjectTrendDetailActivity.start((Activity) context,projectNews.getObjectId());
            }
        }

//        objectType=1，跳回复，指示页
//        objectType=2,就跳项目信息页，就是现在这个，保待不变
//        objectType=3的时候，需要增加一个详情页。
//        数据就是这条动态的详情。。有哪些，就展示哪些

    }
}
